﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Koala.Pro.Common;
using Koala.Pro.Core;
using Koala.Pro.Core.Models;
using Koala.Pro.Helpers;
using Koala.Pro.Models;
using System.Reflection;

namespace Koala.Pro.ViewModels
{
    public partial class DataEntityViewModel : BaseViewModel
    {
        [ObservableProperty]
        private string filePathName = "";

        [ObservableProperty]
        private string assemblyName = "";

        [ObservableProperty]
        private TreeViewModel<CustomTreeNode> treeModel = new TreeViewModel<CustomTreeNode>();

        [ObservableProperty]
        private List<string> checkedList = new List<string>();

        public DataEntityViewModel()
        {
            base.Title = "选择实体类对象";
        }

        [RelayCommand]
        public void GetTree()
        {
            Assembly assembly = null;
            if (!string.IsNullOrEmpty(FilePathName))
            {
                assembly = Assembly.LoadFrom(FilePathName);
                AssemblyName = FileUtil.GetFileNameNoExtension(FilePathName);
            }
            else
            {
                assembly = typeof(UserInfo).Assembly;
                AssemblyName = assembly.GetName().Name;
            }
            CustomTreeNode customTreeNode = new CustomTreeNode
            {
                Name = AssemblyName,
                Id = "-1"
            };
            foreach (Type item2 in from s in assembly.GetTypes()
                                   where s.IsClass
                                   select s)
            {
                if (ReflectionHelper.IsAssignableToGenericType(item2, typeof(IEntity<>)))
                {
                    CustomTreeNode item = new CustomTreeNode
                    {
                        Id = item2.FullName,
                        Name = item2.FullName
                    };
                    customTreeNode.Children.Add(item);
                }
            }
            List<CustomTreeNode> list = new List<CustomTreeNode> { customTreeNode };
            TreeModel.TreeItems = list;
            TreeModel.FilteredTreeItems = new List<CustomTreeNode>(list);
        }

        [RelayCommand]
        public void LoadFile()
        {
            string value = FileDialogHelper.Open("打开程序集", "程序集文件(*.dll)|*.dll|All File(*.*)|*.*", "", AppDomain.CurrentDomain.BaseDirectory);
            if (!string.IsNullOrEmpty(value))
            {
                FilePathName = value;
                GetTree();
            }
        }

        private void GetCheckedList(CustomTreeNode parentNode)
        {
            if (parentNode.Children.Count == 0)
            {
                return;
            }
            foreach (CustomTreeNode child in parentNode.Children)
            {
                if (child.IsChecked == true)
                {
                    CheckedList.Add(child.Name);
                }
                GetCheckedList(child);
            }
        }

        [RelayCommand]
        public void Confirm()
        {
            bool success = false;
            string error = "";
            try
            {
                CheckedList.Clear();
                foreach (CustomTreeNode treeItem in TreeModel.TreeItems)
                {
                    GetCheckedList(treeItem);
                }
                if (CheckedList.Count == 0)
                {
                    MessageDxUtil.ShowTips("没有选择业务对象类，请选择");
                    return;
                }
                success = true;
            }
            catch (Exception ex)
            {
                LogTextHelper.Error(ex);
                error = ex.Message;
            }
            base.Result = new CommonResult(success, error);
        }
    }
}
